<?php

namespace CHOMP\SGIBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use CHOMP\SGIBundle\Entity\Usuario;

class GrupoSoporteType extends AbstractType
{	
	private $id;
	private $esAdmin;
	
	public function __construct($id, $esAdmin){
    	$this->id = $id;
		$this->esAdmin = $esAdmin;
	}    

	/**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
		$builder
		->add('nombre')
		->add('descripcion');
		if($this->esAdmin == 'SI'){
			$builder->add('empresa');
		}else{
			$builder->add('empresa', 'entity', array(
				 'class'=>'CHOMPSGIBundle:Empresa',
				 'property'=>'nombre',
				 'query_builder' => function (\CHOMP\SGIBundle\Entity\EmpresaRepository $repository)
				 {
					 return $repository->createQueryBuilder('u')
							->where('u.id = ?1 AND u.borrado = 0')
							->setParameter(1, $this->id);
				 }
				));
		 }
		if($this->esAdmin != 'SI'){
			$builder->add('usuarios', 'entity', array(
				 'class'=>'CHOMPSGIBundle:Usuario',
				 'required' => true,
				 'property'=>'login',
				 'multiple' => true,
				 'query_builder' => function (\CHOMP\SGIBundle\Entity\UsuarioRepository $repository)
				 {
					 return $repository->createQueryBuilder('u')
							->where('u.empresa = ?1 AND u.borrado = 0')
							->setParameter(1, $this->id);
				 }
				));
		}
	}
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'CHOMP\SGIBundle\Entity\GrupoSoporte'
        ));
    }	
    /**
     * @return string
     */
    public function getName()
    {
        return 'chomp_sgibundle_gruposoporte';
    }
}
